/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.stip.server.service;

import com.stip.server.da.UserInfoDA;
import com.stip.server.da.UserMainDA;
import com.stip.server.da.UserTokenDA;
import com.stip.server.entities.Admin;
import com.stip.server.entities.Organization;
import com.stip.server.entities.UserAct;
import com.stip.server.entities.UsersInfo;
import com.stip.server.entities.UsersMain;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author BlueBerry
 */
@Stateless
@LocalBean
@Path("user")
public class UsersInfoService {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @EJB
    UserInfoDA userInfoDA;
    @EJB
    UserTokenDA userTokenDA;
    @EJB
    UserMainDA userMainDA;
//获取参加活动的列表    
@GET
@Path("/{uid}/activities")
@Produces(MediaType.APPLICATION_JSON)
public List<UserAct> getUserActs(@PathParam("uid") String uid){
UsersMain usersMain=userMainDA.findUsersMainById(uid);
    if (usersMain!=null) {
        return  usersMain.getUserActList();
    } else {
        throw  new WebApplicationException(404);
    }
}    

@GET
@Path("/{uid}/orgs")
@Produces(MediaType.APPLICATION_JSON)
public  List<Admin> getOrganizations(@PathParam("uid") String uid,@QueryParam("token") String token){
    if (token==null) {
        throw  new WebApplicationException(401);
    }
    String checkID=userTokenDA.findToken(token);
    if (checkID == null ? uid == null : checkID.equals(uid)) {
        UsersMain usersMain=userMainDA.findUsersMainById(uid);
        if (usersMain!=null) {
            return  usersMain.getAdminList();
        } else {
            throw  new WebApplicationException(404);
        }
    } else {
        throw  new  WebApplicationException(401);
    }

}



@GET
@Path("/{uid}")
@Produces(MediaType.APPLICATION_JSON)
public UsersInfo getUsersInfo(@PathParam("uid") String uid,@QueryParam("token") String token){
    if (token!=null) {
        String checkID=userTokenDA.findToken(token);
        if (checkID == null ? uid == null : checkID.equals(uid)) {
            if (userInfoDA.findUserInfoByID(uid)!=null) {
                return  userInfoDA.findUserInfoByID(uid);
            } else {
                throw  new WebApplicationException(404);
            }
 
        } else {
            throw  new WebApplicationException(401);
        }
    } else {
        throw  new WebApplicationException(401);
    }
}

@POST
@Produces(MediaType.APPLICATION_JSON)
public UsersInfo postUsersInfo(@QueryParam("token") String token,@QueryParam("email")String email,@QueryParam("qq")String qq,@QueryParam("phone")String phone,@QueryParam("abstracts")String abstracts,@QueryParam("dorm")Integer dorm){
    if (token!=null) {
        String checkID=userTokenDA.findToken(token);
        if (checkID!=null) {
            String newUserInfo=userInfoDA.createUserInfo(checkID, email, qq, phone, abstracts, dorm);
            if (newUserInfo==null) {
                throw new WebApplicationException(400);
            }
            return  userInfoDA.findUserInfoByID(newUserInfo);
        } else {
            throw  new WebApplicationException(401);
        }
    }else{
    throw new WebApplicationException(401);
    }

}
@PUT
@Path("/{uid}")
@Produces(MediaType.APPLICATION_JSON)
public UsersInfo putUsersInfo(@QueryParam("uid") String uid,@QueryParam("token") String token,@QueryParam("email")String email,@QueryParam("qq")String qq,@QueryParam("phone")String phone,@QueryParam("abstracts")String abstracts,@QueryParam("dorm")Integer dorm){
    if (token!=null) {
        String checkID=userTokenDA.findToken(token);
        if (checkID == null ? uid == null : checkID.equals(uid)) {
            if (userInfoDA.editUserInfo(uid, email, qq, phone, abstracts, dorm)) {
                return  userInfoDA.findUserInfoByID(uid);
            } else {
                throw  new WebApplicationException(400);
            }
 
        } else {
            throw  new WebApplicationException(401);
        }
    } else {
        throw  new WebApplicationException(401);
    }

};
}
